package com.yupi.springbootinit.controller;

import com.yupi.springbootinit.service.OutboxService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/internal/outbox")
public class OutboxInternalController {

    @Resource
    private OutboxService outboxService;

    public static record EnqueueReq(
            String eventId,
            String eventType,
            String routingKey,
            Map<String, Object> payload
    ) {}

    @PostMapping("/enqueue")
    public Map<String, Object> enqueue(@RequestBody EnqueueReq req) {
        if (req == null || req.eventType() == null || req.routingKey() == null) {
            return Map.of("ok", false, "msg", "eventType and routingKey are required");
        }
        outboxService.enqueue(req.eventId(), req.eventType(), req.routingKey(),
                req.payload() == null ? Map.of() : req.payload());
        return Map.of("ok", true);
    }
}